package com.inspur.edp.ai.flow.core.service;

import com.inspur.edp.ai.flow.api.FlowMetadataService;
import com.inspur.edp.ai.flow.config.FlowConfig;
import com.inspur.edp.ai.flow.metadata.FlowMetadata;
import com.inspur.edp.common.type.utils.JsonSerializeUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * @author lizhaorui
 * @date 2025/8/19
 * @description
 */
@Slf4j
public class FlowMetadataServiceImpl implements FlowMetadataService {

    @Override
    public FlowMetadata retrieve(String id) {
        String dir = FlowConfig.FLOW_METADATA_DIR;
        if (dir == null || dir.length() == 0) {
            throw new RuntimeException("please set flow metadata dir");
        }
        try {
            List<String> contentLines = Files.readAllLines(Paths.get(dir, id + ".json"));
            StringBuilder stringBuilder = new StringBuilder();
            for (String str : contentLines) {
                stringBuilder.append("\n" + str);
            }
            return JsonSerializeUtil.toObject(stringBuilder.toString(), FlowMetadata.class);
        } catch (IOException e) {
            throw new RuntimeException("read file occur exception。");
        }
    }

    @Override
    public String getVersion(String flowId) {
        return this.retrieve(flowId).getVersion();
    }

}
